﻿using App_1.Entites;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace App_1.Data
{
    public class GameManagementDbContext : IdentityDbContext
    {
        public GameManagementDbContext(DbContextOptions<GameManagementDbContext> options) : base(options)
        {
        }

        public DbSet<Player> Player { get; set; }
        public DbSet<Character> Characters { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //扫码当前程序集下所有实现了IEntityTypeConfiguration接口的类加载配置
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

            modelBuilder.Entity<Player>().HasData(DataSeed.Players);
            modelBuilder.Entity<Character>().HasData(DataSeed.Characters);

            modelBuilder.Entity<IdentityRole>()
                .HasData(
                    new IdentityRole
                    {
                        Name = "Guest",
                        NormalizedName = "GUEST"
                    },
                    new IdentityRole
                    {
                        Name = "Administrator",
                        NormalizedName = "ADMINISTRATOR"
                    }
                );
        }
    }
}